//
//  FindThirdViewController.m
//  
//
//  Created by 郭明皓 on 15/10/24.
//
//

#import "FindThirdViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "GMHSurroundModel.h"
#import "GMHsurroundDetailUrl.h"
#import "GMHSurroundDetailCell.h"
#import "GMHSellCell.h"
#import "GMHDataFiled.h"
#import "DKNightVersion.h"
#import "GMH_Share.h"
@interface FindThirdViewController ()<UITableViewDataSource ,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, retain)UIView *headerView;
@property (nonatomic, retain)NSDictionary *detailDic;
@property (nonatomic, retain)NSArray *offerInfoArr;
@property (nonatomic, retain)NSArray *imageArr;
@end

@implementation FindThirdViewController
- (void)dealloc
{
    [_imageArr release];
    [_offerInfoArr release];
    [_tableview release];
    [_headerView release];
    [_detailDic release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableview reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    CGRect rect = CGRectMake(0, 0,
                             self.view.frame.size.width,
                             self.view.frame.size.height);
    
    //tableView创建
    self.tableview = [[UITableView alloc]initWithFrame:rect style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableview];
    [_tableview release];
    self.tableview.delegate = self;
    self.tableview.dataSource =self;;
    //    self.tableview.rowHeight = 50;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    self.tableview.tableHeaderView = self.headerView;
    [_headerView release];
    
    self.tableview.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.tableview.nightSeparatorColor = UIColorFromRGB(0x313131);
    
    [self create];
}
- (void)create
{
    [GMHDataFiled isHaveFileDocument];
    [GMHDataFiled isHaveFileFind];
    NSInteger number = [self.productIds integerValue];
    NSString *strurl = [NSString stringWithFormat:@"http://appapi.yaochufa.com/v2/Product/GetProductInfoById?system=android&machineCode=ffffffff-9317-02f2-ffff-fffff60c2478&productId=%ld&channel=xiaomimarket&version=4.3.1", number];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.detailDic = dic[@"data"];
        
        self.offerInfoArr = [GMHSurroundModel baseModelArry:dic[@"data"][@"offerInfo"]];
        NSLog(@"%@", self.offerInfoArr);
        self.imageArr =  [GMHSurroundModel baseModelArry:dic[@"data"][@"imageList"]];
        NSLog(@"%@", self.imageArr);
        
        
        GMHSurroundModel *surround = self.imageArr[0];
        UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
        [images sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://cdn5.jinxidao.com/%@", surround.url]]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        [self.headerView addSubview:images];
        
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width , 25)];
        [self.headerView addSubview:label1];
        label1.text = self.detailDic[@"productName"];
        label1.backgroundColor = [UIColor whiteColor];
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 175, self.view.frame.size.width , 25)];
        [self.headerView addSubview:label2];
        label2.text = self.detailDic[@"appSubTitle"];
        label2.font = [UIFont systemFontOfSize:12];
        label2.backgroundColor = [UIColor whiteColor];
        
        if ([GMH_Share SingleShare].isNight == YES) {
            self.headerView.backgroundColor = [UIColor blackColor];
            images.alpha = 0.4;
            label1.nightBackgroundColor = UIColorFromRGB(0x343434);
            label2.nightBackgroundColor = UIColorFromRGB(0x343434);
            label1.textColor = [UIColor darkGrayColor];
            label2.textColor = [UIColor darkGrayColor];
        }
        
        [self.tableview reloadData];
        
        NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Find/Titledetail.plist"];
        [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        static NSString *strs = @"tableView1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strs];
        }
        [DKNightVersionManager addClassToSet:self.class];
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        if (self.offerInfoArr.count > 0) {
            GMHSurroundModel *surround = self.offerInfoArr[indexPath.row];
            cell.textLabel.text = surround.flagTitle;
            cell.detailTextLabel.text = surround.flagMsg;
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        }
        
        return cell;
    }
    if (indexPath.section == 1)  {
        static NSString *strs = @"tableView2";
        GMHSellCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[GMHSellCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strs];
        }
        [DKNightVersionManager addClassToSet:self.class];
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);

        if (self.detailDic.count > 0) {
            cell.sellCount.text = [NSString stringWithFormat:@"已售%@", self.detailDic[@"saledCount"]];
            cell.evalua.text = [NSString stringWithFormat:@"%@人评价", self.detailDic[@"commentCount"]];
            cell.sellCount.textColor = [UIColor darkGrayColor];
            cell.evalua.textColor = [UIColor darkGrayColor];

        }
            return cell;

    }
    if (indexPath.section == 2){
        static NSString *strs = @"tableView3";
        GMHSurroundDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[GMHSurroundDetailCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strs];
        }
        [DKNightVersionManager addClassToSet:self.class];
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        if (self.detailDic.count > 0) {
            cell.address.text = [NSString stringWithFormat:@"%@", self.detailDic[@"address"]];
            cell.address.textColor = [UIColor darkGrayColor];
        }
        
        return cell;
        
    }
    if (indexPath.section == 3)    {
        static NSString *strs = @"tableView4";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strs];
        }
        if (self.detailDic.count > 0) {
            [DKNightVersionManager addClassToSet:self.class];
            cell.nightBackgroundColor = UIColorFromRGB(0x343434);
            cell.textLabel.text = @"出行路线";
            cell.textLabel.textColor = [UIColor darkGrayColor];
        }
        
            return cell;

    }
    if (indexPath.section == 4) {
        static NSString *strs = @"tableView5";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strs];
        }
        
        [DKNightVersionManager addClassToSet:self.class];
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        if (self.detailDic.count > 0) {
            cell.textLabel.text = @"详情介绍";
            cell.textLabel.textColor = [UIColor darkGrayColor];
        }
        
        return cell;
    }
    else
    {
        static NSString *strs = @"tableView6";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strs];
        }
        [DKNightVersionManager addClassToSet:self.class];
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        if (self.detailDic.count > 0) {
            cell.textLabel.text = @"购买须知";
            cell.textLabel.textColor = [UIColor darkGrayColor];

        }
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//        if (indexPath.section == 2 && indexPath.row == 0) {
//            [tableView deselectRowAtIndexPath:indexPath animated:YES];
//            GMHsurroundDetailUrl *url = [[GMHsurroundDetailUrl alloc]init];
//            [self.navigationController pushViewController:url animated:YES];
//            [url release];
//            NSInteger number = [self.productIds integerValue];
//             url.number = number;
//
//        url.url = self.detailDic[@"recommendTripStr"];
//    }
    if (indexPath.section == 3 && indexPath.row == 0) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        GMHsurroundDetailUrl *url = [[GMHsurroundDetailUrl alloc]init];
        [self.navigationController pushViewController:url animated:YES];
        [url release];
        NSInteger number = [self.productIds integerValue];
        url.number = number;

        url.url = self.detailDic[@"trafficGuide"];
    }
    if (indexPath.section == 4  && indexPath.row == 0) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        GMHsurroundDetailUrl *url = [[GMHsurroundDetailUrl alloc]init];
        [self.navigationController pushViewController:url animated:YES];
        [url release];
        NSInteger number = [self.productIds integerValue];
        url.number = number;

        
        url.url = self.detailDic[@"productIntroduction"];
    }
    if (indexPath.section == 5 && indexPath.row == 0) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        GMHsurroundDetailUrl *url = [[GMHsurroundDetailUrl alloc]init];
        [self.navigationController pushViewController:url animated:YES];
        [url release];
        NSInteger number = [self.productIds integerValue];
        url.number = number;

        url.url = self.detailDic[@"packageDetial"];
    }
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
